package org.example.controller.impl;

import org.example.domain.Goods;
import org.example.domain.response.ResponseResult;
import org.example.services.impl.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/goods")
public class GoodsControllerImpl {
    @Autowired
    private GoodsServiceImpl goodsService;


//    添加商品
    @PostMapping
    @PreAuthorize("hasAuthority('goods:save')")
    public ResponseResult saveGoods(@RequestBody @Validated(Goods.Save.class) Goods goods){
        return goodsService.saveGoods(goods);
    }

    //    修改商品
    @PutMapping
    @PreAuthorize("hasAuthority('goods:update')")
    public ResponseResult UpdateGoods(@RequestBody  Goods goods){
        return goodsService.updateGoods(goods);
    }
//    删除商品
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('goods:delete')")
    public ResponseResult deleteGoods(@PathVariable("id") Integer goodsId){
       return goodsService.deleteGoods(goodsId);
    }
//    通过id查询商品
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('goods:selectOne')")
    public ResponseResult getGoodsByGoodsId(@PathVariable("id") int goodsId){
        return goodsService.getGoodsByGoodsId(goodsId);

    }
//    查询所有
    @GetMapping
//    @PreAuthorize("hasAuthority('goods:selectAll')")
    public ResponseResult getGoods(){
        return goodsService.getAllGoods();
    }

//    分页查询商品信息
    @GetMapping("/3/{pageSize}/{currentPage}")
    public ResponseResult getGoodsByPage(@PathVariable int pageSize, @PathVariable int currentPage) {
        return goodsService.getGoodsByPage(currentPage, pageSize);

    }

//    通过name查询
    @GetMapping("/1/{name}/{pageSize}/{currentPage}")
    @PreAuthorize("hasAuthority('goods:selectAll')")
    public ResponseResult getPageByName(@PathVariable String name,@PathVariable int pageSize,@PathVariable int currentPage){
        return goodsService.getGoodsByName(name,pageSize,currentPage);
    }

    //    通过type查询
    @GetMapping("/2/{type}/{pageSize}/{currentPage}")
    @PreAuthorize("hasAuthority('goods:selectAll')")
    public ResponseResult getPageByType(@PathVariable String type,@PathVariable int pageSize,@PathVariable int currentPage){
        return goodsService.getGoodsByType(type,pageSize,currentPage);
    }

    @PostMapping("/img/{goodsId}")
    @PreAuthorize("hasAuthority('goods:update')")
    public ResponseResult uploadGoodsImg(@RequestParam("file") MultipartFile file,@PathVariable int goodsId){
        return goodsService.uploadGoodsImg(file,goodsId);
    }
}
